package com.joven.shelltest.utils.pdf;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.draw.LineSeparator;
import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.Objects;

/**
 * PDF建造者模式
 *
 * @Author wzj
 * @Date 2023-08-24 08:42
 **/
@Slf4j
public class PdfBuilder extends PdfBase {
    /**
     * 空行
     */
    private static final Paragraph BLANK = new Paragraph(" ");

    /**
     * 粗线
     */
    private static final LineSeparator BOLD_LINE = new LineSeparator(2f, 100, BaseColor.BLACK, Element.ALIGN_CENTER, 0f);

    /**
     * 粗线
     */
    private static final LineSeparator LINE = new LineSeparator(1f, 100, BaseColor.BLACK, Element.ALIGN_CENTER, 0f);

    /**
     * 文档对象
     */
    private Document document;

    private PdfWriter writer;

    /**
     * 构造类，使用默认纸张A4
     *
     * @param filePath 文件路径
     * @return com.ah.ums.service.pdf.PdfBuilder
     */
    public static PdfBuilder builder(String filePath) throws DocumentException, FileNotFoundException {
        return new PdfBuilder(filePath);
    }

    public static PdfBuilder builder() throws DocumentException, FileNotFoundException {
        return new PdfBuilder();
    }

    /**
     * 构造类
     *
     * @param filePath 文件路径
     * @param pageSize 指定纸张
     * @return com.ah.ums.service.pdf.PdfBuilder
     */
    public static PdfBuilder builder(String filePath, Rectangle pageSize) throws DocumentException, FileNotFoundException {
        return new PdfBuilder(filePath, pageSize);
    }

    /**
     * 构造函数，默认纸张A4
     */
    private PdfBuilder(String filePath) throws FileNotFoundException, DocumentException {
        init(filePath, PageSize.A4);
    }

    private PdfBuilder() throws FileNotFoundException, DocumentException {
        init(PageSize.A4);
    }

    /**
     * 构造函数，传入纸张
     */
    private PdfBuilder(String filePath, Rectangle pageSize) throws FileNotFoundException, DocumentException {
        init(filePath, pageSize);
    }

    /**
     * 初始化Pdf
     *
     * @param filePath pdf文档地址
     * @param pageSize 文档页大小
     * @return void
     */
    private void init(String filePath, Rectangle pageSize) throws FileNotFoundException, DocumentException {
        document = new Document(pageSize);
        // 为document创建一个监听，并把PDF流写到文件中
        // 文件的输出路径+文件的实际名称
        writer = PdfWriter.getInstance(document, new FileOutputStream(filePath));
        // 打开文档
        document.open();
        log.debug("{}", document);
    }

    private ByteArrayOutputStream outputStream;

    private void init(Rectangle pageSize) throws FileNotFoundException, DocumentException {
        document = new Document(pageSize);
        // 为document创建一个监听，并把PDF流写到文件中
        // 文件的输出路径+文件的实际名称
        outputStream = new ByteArrayOutputStream();
        writer = PdfWriter.getInstance(document, outputStream);
        // 打开文档
        document.open();
        log.debug("{}", document);
    }


    /**
     * 写文档标题
     *
     * @param title
     * @return com.ah.ums.service.pdf.PdfBuilder
     */
    public PdfBuilder title(String title) throws DocumentException {
        title(BaseColor.BLACK, title);
        return this;
    }

    /**
     * 写文档标题带颜色
     *
     * @param color 自定义颜色
     * @param title
     * @return com.ah.ums.service.pdf.PdfBuilder
     */
    public PdfBuilder title(BaseColor color, String title) throws DocumentException {
        document.add(new Paragraph(title, PdfFontBuilder.builder().fontSize(TITLE_FONT_SIZE).fontStyle(Font.BOLD).fontColor(color).build()));
        return this;
    }

    /**
     * 写文档标题带颜色
     *
     * @param color
     * @param title
     * @param alignment 1 2 3代表 中 右 左
     * @return
     * @throws DocumentException
     */
    public PdfBuilder title(BaseColor color, String title, int alignment) throws DocumentException {
        Paragraph paragraph = new Paragraph(title, PdfFontBuilder.builder().fontSize(TITLE_FONT_SIZE).fontStyle(Font.BOLD).fontColor(color).build());
        paragraph.setAlignment(alignment);
        document.add(paragraph);
        return this;
    }

    public PdfBuilder logoTitle(String logoPath) throws DocumentException, IOException {
        PdfContentByte contentByte = writer.getDirectContent();
        Image image = Image.getInstance(logoPath);
        image.scaleAbsolute(30, 30);
        image.setAbsolutePosition(20, PageSize.A4.getHeight() - 40);
        contentByte.addImage(image);

//        BaseFont baseFont = BaseFont.createFont("STSongStd-Light" ,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
//        contentByte.beginText();
//        contentByte.setFontAndSize(baseFont, 12);
//        contentByte.showTextAligned(PdfContentByte.ALIGN_LEFT,"浙江信安数智科技有限公司", 50, 30, 0);
//        contentByte.endText();

        // 添加文字
        Font font9 = PdfBase.PdfFontBuilder.builder().fontSize(8).build();
        Paragraph paragraph = new Paragraph("浙江信安数智科技有限公司", font9);
        paragraph.setLeading(12);
        paragraph.setAlignment(Paragraph.ALIGN_LEFT);
        paragraph.setIndentationLeft(30);
        paragraph.setIndentationRight(50);
        document.add(paragraph);
        return this;
    }


    /**
     * 写文档小标题
     *
     * @param title
     * @return com.ah.ums.service.pdf.PdfBuilder
     */
    public PdfBuilder smallTitle(String title) throws DocumentException {
        smallTitle(BaseColor.BLACK, title);
        return this;
    }

    /**
     * 写文档小标题
     *
     * @param color 自定义颜色
     * @param title
     * @return com.ah.ums.service.pdf.PdfBuilder
     */
    public PdfBuilder smallTitle(BaseColor color, String title) throws DocumentException {
        document.add(new Paragraph(title, PdfFontBuilder.builder().fontSize(SMALL_TITLE_FONT_SIZE).fontStyle(Font.BOLD).fontColor(color).build()));
        return this;
    }

    /**
     * 写文档小标题
     *
     * @param color     自定义颜色
     * @param title
     * @param alignment 对齐方式
     * @return com.ah.ums.service.pdf.PdfBuilder
     * @throws DocumentException
     */
    public PdfBuilder smallTitle(BaseColor color, String title, int alignment) throws DocumentException {
        Paragraph paragraph = new Paragraph(title, PdfFontBuilder.builder().fontSize(SMALL_TITLE_FONT_SIZE).fontStyle(Font.BOLD).fontColor(color).build());
        paragraph.setAlignment(alignment);
        document.add(paragraph);
        return this;
    }

    /**
     * 空行
     *
     * @return com.ah.ums.service.pdf.PdfBuilder
     */
    public PdfBuilder blank() throws DocumentException {
        document.add(BLANK);
        return this;
    }


    /**
     * 粗横线
     *
     * @return com.ah.ums.service.pdf.PdfBuilder
     */
    public PdfBuilder boldLine() throws DocumentException {
        document.add(BOLD_LINE);
        return this;
    }


    /**
     * 横线
     *
     * @return com.ah.ums.service.pdf.PdfBuilder
     */
    public PdfBuilder line() throws DocumentException {
        document.add(LINE);
        return this;
    }

    /**
     * 写文本内容
     *
     * @param text 文本内容
     * @return com.ah.ums.service.pdf.PdfBuilder
     */
    public PdfBuilder text(String text) throws DocumentException {
        document.add(new Paragraph(text, PdfFontBuilder.builder().build()));
        return this;
    }

    /**
     * 指定字体大小写入
     *
     * @param fontSize 字体大小
     * @param text     文本内容
     * @return com.ah.ums.service.pdf.PdfBuilder
     */
    public PdfBuilder text(int fontSize, String text) throws DocumentException {
        document.add(new Paragraph(text, PdfFontBuilder.builder().fontSize(fontSize).build()));
        return this;
    }

    /**
     * 指定字体大小写入
     *
     * @param fontSize 字体大小
     * @param color    自定义颜色
     * @param text     文本内容
     * @return com.ah.ums.service.pdf.PdfBuilder
     */
    public PdfBuilder text(int fontSize, BaseColor color, String text) throws DocumentException {
        document.add(new Paragraph(text, PdfFontBuilder.builder().fontSize(fontSize).fontColor(color).build()));
        return this;
    }

    public PdfBuilder text(int fontSize, BaseColor color, String text, int alignment, Integer left, Integer right) throws DocumentException {
        Paragraph paragraph = new Paragraph(text, PdfFontBuilder.builder().fontSize(fontSize).fontColor(color).build());
        paragraph.setAlignment(alignment);
        if (Objects.nonNull(left)) {
            paragraph.setIndentationLeft(left);
        }
        if (Objects.nonNull(right)) {
            paragraph.setIndentationRight(right);
        }
        document.add(paragraph);
        return this;
    }

    public PdfBuilder separator() {
        PdfContentByte canvas = writer.getDirectContent();
        canvas.moveTo(36, document.top() - 20);
        //559 680
        canvas.lineTo(document.right() - 36, document.top() - 20);
        canvas.stroke();
        return this;
    }


    /**
     * 指定字体大小写入
     *
     * @param fontSize  字体大小
     * @param fontStyle 字体样式
     * @param color     自定义颜色
     * @param text      文本内容
     * @return com.ah.ums.service.pdf.PdfBuilder
     */
    public PdfBuilder text(int fontSize, int fontStyle, BaseColor color, String text) throws DocumentException {
        document.add(new Paragraph(text,
                PdfFontBuilder.builder()
                        .fontSize(fontSize)
                        .fontStyle(fontStyle)
                        .fontColor(color)
                        .build()));
        return this;
    }

    public PdfBuilder text(int fontSize, int fontStyle, BaseColor color, String text, int alignment) throws DocumentException {
        Paragraph paragraph = new Paragraph(text, PdfFontBuilder.builder().fontSize(fontSize).fontStyle(fontStyle).fontColor(color).build());
        paragraph.setAlignment(alignment);
        document.add(paragraph);
        return this;
    }

    /**
     * 指定字体大小写入
     *
     * @param font 外部传入字体
     * @param text 文本内容
     * @return com.ah.ums.service.pdf.PdfBuilder
     */
    public PdfBuilder text(Font font, String text) throws DocumentException {
        document.add(new Paragraph(text, font));
        return this;
    }

    /**
     * 签章
     *
     * @param imgPath 签章路径
     * @return com.ah.ums.service.pdf.PdfBuilder
     */
    public PdfBuilder sign(String imgPath) throws DocumentException, IOException {
        sign(150, imgPath);
        return this;
    }

    /**
     * 签章
     *
     * @param scale   缩放
     * @param imgPath 签章路径
     * @return com.ah.ums.service.pdf.PdfBuilder
     */
    public PdfBuilder sign(int scale, String imgPath) throws DocumentException, IOException {
        // 获取图片
        Image img = Image.getInstance(imgPath);
        // 靠右
        img.setAlignment(Image.RIGHT);
        img.scaleToFit(scale, scale);
        img.setIndentationRight(50);
        //img.setAbsolutePosition(420,100);
        document.add(img);
        return this;
    }

    /**
     * 写入表格
     *
     * @param pdfPTable 表格
     * @return com.ah.ums.service.pdf.PdfBuilder
     */
    public PdfBuilder table(PdfPTable pdfPTable) throws DocumentException {
        document.add(pdfPTable);
        return this;
    }

    public void build() {
        document.close();
        writer.close();
    }

    public InputStream specialBuild() {
        document.close();
        return new ByteArrayInputStream(outputStream.toByteArray());
    }

}
